package zy1220.zy4;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demo {
    static Map<String,Course> cmap = new HashMap<>();
    static Map<String,Student> smap = new HashMap<>();
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        Document cread = reader.read("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy4\\course.xml");
        Element r1 = cread.getRootElement();
        List<Element> clist = r1.elements();
        for (Element e : clist) {
            cmap.put(e.attributeValue("id"),new Course(e.attributeValue("id"),e.elementText("name")));
        }
        Document sread = reader.read("D:\\ideadashuju12\\liu_ya_kai\\src\\zy1220\\zy4\\student.xml");
        Element r2 = sread.getRootElement();
        List<Element> slist = r2.elements();
        for (Element e : slist) {
            smap.put(e.attributeValue("id"),new Student(e.attributeValue("id"),e.attributeValue("name")
                    ,Integer.parseInt(e.attributeValue("age")),e.attributeValue("course")));
        }
        for (String s : cmap.keySet()) {
            for (String s1 : smap.keySet()) {
                if (s.equals("c001") && s.equals(smap.get(s1).getCourse())){
                    System.out.println(smap.get(s1).getId()+" "+smap.get(s1).getName()
                            +" "+smap.get(s1).getAge()+" "+smap.get(s1).getCourse());
                }
            }
        }
        System.out.println("===================================================");

        HashMap<String, Integer> map = new HashMap<>();
        for (String s : smap.keySet()) {
            if (map.containsKey(smap.get(s).getCourse())){
                map.put(smap.get(s).getCourse(),map.get(smap.get(s).getCourse())+1);
            }else {
                map.put(smap.get(s).getCourse(),1);
            }
        }

        System.out.println(map);
        System.out.println("===================================================");

        for (String s : cmap.keySet()) {
            for (String s1 : smap.keySet()) {
                if (s.equals("c001") && s.equals(smap.get(s1).getCourse()) && smap.get(s1).getAge()==18){
                    System.out.println(smap.get(s1).getId()+" "+smap.get(s1).getName()
                            +" "+smap.get(s1).getAge()+" "+smap.get(s1).getCourse());
                }
            }
        }
    }
}
